//
//  LostViewController.m
//  SmartCup
//
//  Created by Kendama on 2017/5/18.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "LostViewController.h"
#import "LostTableViewCell.h"

@interface LostViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSArray *lostList;

/** 背景图 **/
@property(nonatomic,strong) UIImageView *backbroundView;
@property(nonatomic,strong) UILabel *tipsLabel;
@property(nonatomic,strong) UIView *noMachineView;


@end

@implementation LostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)refreshBaseControlValue {
    [self setNavigationTitle:@"防丢报警" titleColor:[UIColor whiteColor]];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"3E76F6"];
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
    [self setRightButtonImage:[UIImage imageNamed:@"icon_add"]];

    self.lostList = [LostModel selectAllObject];
    
    [self masLayout];

    [self checkData];
}

- (void)checkData {
    if (self.lostList.count == 0) {
        [self.view bringSubviewToFront:self.noMachineView];
    } else {
        [self.view bringSubviewToFront:self.tableView];
    }
}

#pragma mark -- 事件
- (void)onRightButtonClick:(id)sender {
    self.hidesBottomBarWhenPushed = YES;
    LostSettingViewController *vc = [[LostSettingViewController alloc] init];
    vc.type = 1;
    vc.updateBlock = ^{

        self.lostList = [LostModel selectAllObject];
        [self.tableView reloadData];
        [self checkData];

    };
    [self.navigationController pushViewController:vc animated:YES];
//    self.hidesBottomBarWhenPushed = NO;

}

#pragma mark -- 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //    NSArray *array = self.listArray[section];
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.lostList.count;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return px1920Height(340);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return px1920Height(40);
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LostTableViewCell *cell = [LostTableViewCell cellWithTableView:tableView];
    LostModel *lost = self.lostList[indexPath.section];
    cell.lost = lost;
    cell.switchBlock = ^(BOOL switchMark){
        lost.switchMark = switchMark;
        [lost updateObject];
        [self.tableView reloadData];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.hidesBottomBarWhenPushed = YES;
    LostSettingViewController *vc = [[LostSettingViewController alloc] init];
//    vc.dict = self.lostList[indexPath.section];
    LostModel *lost = self.lostList[indexPath.section];
    vc.lostModel = lost;
    vc.updateBlock = ^{
        self.lostList = [LostModel selectAllObject];
        [self.tableView reloadData];
        [self checkData];
    };
    [self.navigationController pushViewController:vc animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark -- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [superView addSubview:_tableView];
    }
    return _tableView;
}


- (void) masLayout{
    __weak UIView *superView = self.view;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(64);
        
        make.left.right.bottom.equalTo(self.view);
    }];
    
    [self.noMachineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.width.mas_equalTo(superView);
        make.bottom.mas_equalTo(superView.mas_bottom);
    }];
    
    [self.backbroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(272));
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.width.mas_equalTo(px1080Width(456));
        make.height.mas_equalTo(px1920Height(456));
    }];
    
    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backbroundView.mas_bottom).with.offset(px1920Height(40));
        make.width.mas_equalTo(superView);
        make.height.mas_equalTo(px1920Height(73));
    }];
}

- (NSArray *)lostList
{
    if(!_lostList)
    {
        _lostList = [[NSArray alloc]init];
    }
    return _lostList;
}


- (UILabel *)tipsLabel
{
    if(!_tipsLabel)
    {
        UIView *superView = self.noMachineView;
        _tipsLabel = [[UILabel alloc]init];
        [superView addSubview:_tipsLabel];
        [_tipsLabel setFont:[UIFont systemFontOfSize:px1080Width(52)]];
        [_tipsLabel setTextColor:[UIColor colorWithHexString:@"#585D6D"]];
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
        _tipsLabel.text = @"您好像还没设置防丢时段";
    }
    return _tipsLabel;
}

- (UIImageView *)backbroundView
{
    if(!_backbroundView)
    {
        UIView *superView = self.noMachineView;
        _backbroundView = [[UIImageView alloc]init];
        [superView addSubview:_backbroundView];
        _backbroundView.image = [UIImage imageNamed:@"bg_equipment"];
    }
    return _backbroundView;
}

- (UIView *)noMachineView
{
    if(!_noMachineView)
    {
        UIView *superView = self.view;
        _noMachineView = [[UIView alloc]init];
        _noMachineView.backgroundColor = [UIColor whiteColor];
        [superView addSubview:_noMachineView];
    }
    return _noMachineView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
